# 完整修复video_processor.py文件
import os

# 文件路径
file_path = r'd:\未知\yolo_apply\video_processing\video_processor.py'

# 读取整个文件
with open(file_path, 'r', encoding='utf-8') as f:
    content = f.read()

# 查找错误区域并替换
# 使用更精确的搜索模式
search_text = '''                # 错误率过高时触发警告和恢复
                current_time = time.time()
                if error_count > 10 and (current_time - last_error_time > 30):
                    logger.warning(f"处理错误率过高: {self.camera_id}, 最近错误数: {error_count}")
                    if hasattr(self, '_handle_error'):
                        self._handle_error(Exception(f'30秒内出现{error_count}个错误'), 'high_error_rate')
                    last_error_time = current_time
                    error_count = 0'''

# 替换为新的代码，确保没有语法问题
replace_text = '''                # 错误率过高时触发警告和恢复
                current_time_value = time.time()
                if error_count > 10 and (current_time_value - last_error_time > 30):
                    logger.warning("处理错误率过高: " + str(self.camera_id) + ", 最近错误数: " + str(error_count))
                    if hasattr(self, '_handle_error'):
                        self._handle_error(Exception('30秒内出现' + str(error_count) + '个错误'), 'high_error_rate')
                    last_error_time = current_time_value
                    error_count = 0'''

# 如果没有找到精确匹配，尝试另一种搜索方式
if search_text not in content:
    # 尝试查找包含注释的行附近的代码
    if '# 错误率过高时触发警告和恢复' in content:
        lines = content.split('\n')
        for i, line in enumerate(lines):
            if '# 错误率过高时触发警告和恢复' in line:
                # 替换这一行及后面的7行
                start = i
                end = min(i + 8, len(lines))
                new_lines = lines[:start] + replace_text.split('\n') + lines[end:]
                content = '\n'.join(new_lines)
                break

# 备份原文件
backup_path = file_path + '.backup'
os.rename(file_path, backup_path)

# 写入修复后的内容
with open(file_path, 'w', encoding='utf-8') as f:
    f.write(content)

print(f"已创建备份文件: {backup_path}")
print(f"已成功修复 {file_path} 中的语法错误")